/* *****************************************************************************************
 
Made by www.Flashphoner.com, all rights reserved.
These sources can only be used for working with Flashphoner server.
If you want use this to create your own application (separate from Flashphoner server) � 
please contact support@flashphoner.com 

******************************************************************************************/

package com.flashphoner.api
{
		
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.flashphoner.Logger;
	import com.flashphoner.api.data.Call;
	import com.flashphoner.api.data.ModelLocator;
	import com.flashphoner.api.data.PhoneConfig;
	import com.flashphoner.api.management.SoundControl;
	
	import flash.events.*;
	import flash.utils.*;
	
	internal class PhoneCommand implements ICommand
	{		
		private var hangupTimer:Timer;
		
		private var flashAPI:Flash_API = Flash_API.getInstance();		
				
									
		public function PhoneCommand()
		{
		}	
				
		public function execute( event : CairngormEvent ) : void
		{	
			Logger.info("PhoneCommand.execute() event.type "+event.type);
			if (event.type==MainEvent.TALK){
				Logger.info("MainEvent.TALK");
				SoundControl.getInstance().stopRingSound();				
				var call:Call = (event as MainEvent).obj as Call;											
				Logger.info("MainEvent.TALK "+call.id);
				flashAPI.phoneServerProxy.publish(call);						
				
				flashAPI.phoneServerProxy.phoneSpeaker.play("INCOMING_"+flashAPI.modelLocator.login+"_"+call.id);				
				if (PhoneConfig.VIDEO_ENABLED){
					flashAPI.phoneServerProxy.phoneSpeaker.playVideo("VIDEO_INCOMING_"+flashAPI.modelLocator.login+"_"+call.id);
				}
			}
			 
			if (event.type == MainEvent.SESSION_PROGRESS){
				Logger.info("MainEvent.SESSION_PROGRESS");
				var call:Call = (event as MainEvent).obj as Call;
		 		flashAPI.currentCall = call;
		 		SoundControl.getInstance().stopRingSound();	
		 		flashAPI.phoneServerProxy.publish(call);	 		
		 		flashAPI.phoneServerProxy.phoneSpeaker.play("INCOMING_"+flashAPI.modelLocator.login+"_"+call.id);
		 	}
			
			if (event.type==MainEvent.IN){
				Logger.info("MainEvent.IN");						
				SoundControl.getInstance().playRingSound();
			}
			
			if (event.type ==MainEvent.OUT){				
				Logger.info("MainEvent.OUT");
				SoundControl.getInstance().playRingSound();
			}
			if (event.type == MainEvent.BUSY){
				var call:Call = (event as MainEvent).obj as Call;
				
				SoundControl.getInstance().playBusySound();		
				finish(call);	
			}				
			
			if (event.type == MainEvent.FINISH){
				var call:Call = (event as MainEvent).obj as Call;
				SoundControl.getInstance().playFinishSound();
				finish(call);
			}			
			
			if (event.type == MainEvent.CONNECTED){				
				if (!PhoneConfig.REGISTER_REQUIRED){				
					flashAPI.modelLocator.logged = true;		
				}						
			}
			if (event.type == MainEvent.REGISTERED){
				var modelLocator:ModelLocator = Flash_API.getInstance().modelLocator;
				if (modelLocator.mode != "click2call"){
					SoundControl.getInstance().playRegisterSound();
				}
				PhoneModel.getInstance().registered=true;
				flashAPI.modelLocator.logged = true;		
				PhoneModel.getInstance().dropRegisteredTimer();
			}			
			
		}
		
		private function finish(call:Call):void {
				SoundControl.getInstance().stopRingSound();														
				flashAPI.phoneServerProxy.unpublish(call);	
				flashAPI.currentCall=null;
				flashAPI.phoneServerProxy.phoneSpeaker.stop();
		}
		
	}
}